/*
 * Copyright (c) 2012 Johann Hanne
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <stdint.h>
#include <putchar.h>
#include <string.h>
#include <ethernet.h>
#include <delay.h>

union {
    uint32_t ethernet_alignment_enforcement;
    uint8_t data[1514];
} ethernet_ip_udp_frame = { .data = {
    /* Destination ethernet address - multicast mapping for 239.192.1.1 */
    0x01, 0x00, 0x5e, 0x40, 0x01, 0x01,
    /* Source ethernet address */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    /* ethertype IPv4 */
    0x08, 0x00,
    /* IP version 4, header length */
    0x45,
    /* DSCP/ECN */
    0x00,
    /* Total length (IP header + UDP header + payload */
    0x00, 0x00, /* must be changed dynamically */
    /* Identification */
    0x12, 0x34,
    /* Flags (Don't fragment), fragment offset */
    0x40, 0x00,
    /* TTL */
    0x40,
    /* Protocol: UDP */
    0x11,
    /* Header checksum */
    0x00, 0x00, /* must be changed dynamically */
    /* Source */
    192, 168, 9, 1,
    /* Destination */
    239, 192, 1, 1,
    /* Source port */
    0xff, 0xff,
    /* Destination port 1234 */
    0x04, 0xd2,
    /* Length (UDP header + payload) */
    0x00, 0x00, /* must be changed dynamically */
    /* Checksum (0x0000 means "checksum unused") */
    0x00, 0x00,
    /* Hello world! */
    0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21
}};

static void set_udp_ip_length(uint8_t *packet, uint16_t payload_length) {
    uint16_t total_length = payload_length + 28;
    uint16_t udp_length = payload_length + 8;
    uint16_t ip_chksum = 0xffff;
    uint8_t *p;
    uint16_t w;

    packet[16] = (total_length & 0xff00) >> 8;
    packet[17] =  total_length & 0x00ff;

    packet[38] = (udp_length & 0xff00) >> 8;
    packet[39] =  udp_length & 0x00ff;

    packet[24] = 0x00;
    packet[25] = 0x00;

    for (p = packet + 14;
         p < packet + 34;
         p += 2) {
        w = ((uint16_t)(*p) << 8) | (uint16_t)(*(p + 1));
        if (w > ip_chksum) {
            w++;
        }
        ip_chksum -= w;
    }

    packet[24] = (ip_chksum & 0xff00) >> 8;
    packet[25] =  ip_chksum & 0x00ff;
}

void rx(uint8_t *data, size_t len) {
    putchar('!');
}

void main(void) {
    ethernet_init();
    ethernet_get_mac_address(ethernet_ip_udp_frame.data + 6);
    set_udp_ip_length(ethernet_ip_udp_frame.data, 12);
    for (;;) {
        putchar('.');
        ethernet_tx(ethernet_ip_udp_frame.data, sizeof(ethernet_ip_udp_frame.data));
        ethernet_rx(rx);
        delay_ms(1000);
    }
}
